package com.kzj.mls.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.kzj.mls.R;
import com.kzj.mls.adapter.OrderFragmentAdapter;
import com.kzj.mls.beans.MoneyControlBean;
import com.kzj.mls.fragment.AllMoneyCalculateFragment;
import com.kzj.mls.fragment.AllOrderFragment;

import com.kzj.mls.global.App;
import com.kzj.mls.recyclerCard.basic.MaterialListView;
import com.kzj.mls.recyclerCard.card.other.AboutCard;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;

import de.greenrobot.event.EventBus;

/**
 * Created by xyg on 2015/11/3.
 */
public class MoneyActivity extends GBaseActivity implements ViewPager.OnPageChangeListener{
    private MaterialListView material_listview;
    private android.support.v4.view.ViewPager viewPager_viewpager;
    private ArrayList<Fragment> fragmentList;
    private OrderFragmentAdapter orderFragmentAdapter;
    private TextView textView_djsbs1;
    private TextView textView_djsje1;
    private TextView textView_djsbs2;
    private TextView textView_djsje2;
    private TextView textView_djsbs3;
    private TextView textView_djsje3;
    private View view1, view2, view3, view4;
    private TextView textView_all_pay;
    private TextView textView_wait_pay;
    private TextView textView_can_pay;
    private TextView textView_end_pay;
    public static final String qb="0";
    public static final String djs="1";
    public static final String kjs="2";
    public static final String yjs="3";
    // 0全部 1'待结算', '2可结算', '3已结算'
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentViewFor720_1280(R.layout.activity_money);
        findViewByIds();
        initDatas();
        fillArrayInListView();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void findViewByIds() {
        material_listview = (MaterialListView) findViewById(R.id.material_listview);
        viewPager_viewpager= (ViewPager) findViewById(R.id.viewPager_viewpager);
        viewPager_viewpager.setOffscreenPageLimit(4);
        viewPager_viewpager.setOnPageChangeListener(this);
        view1 = findViewById(R.id.view1);
        view2 = findViewById(R.id.view2);
        view3 = findViewById(R.id.view3);
        view4 = findViewById(R.id.view4);
        textView_djsbs1 = (TextView) findViewById(R.id.textView_djsbs1);
        textView_djsje1 = (TextView) findViewById(R.id.textView_djsje1);
        textView_djsbs2 = (TextView) findViewById(R.id.textView_djsbs2);
        textView_djsje2 = (TextView) findViewById(R.id.textView_djsje2);
        textView_djsbs3 = (TextView) findViewById(R.id.textView_djsbs3);
        textView_djsje3 = (TextView) findViewById(R.id.textView_djsje3);
        textView_all_pay = (TextView) findViewById(R.id.textView_all_pay);textView_all_pay.setOnClickListener(this);
        textView_wait_pay = (TextView) findViewById(R.id.textView_wait_pay);textView_wait_pay.setOnClickListener(this);
        textView_can_pay = (TextView) findViewById(R.id.textView_can_pay);textView_can_pay.setOnClickListener(this);
        textView_end_pay = (TextView) findViewById(R.id.textView_end_pay);textView_end_pay.setOnClickListener(this);
    }

    @Override
    protected void initDatas() {
        setTopTitle("资金");
        setLeftImg(View.VISIBLE, R.drawable.back);
        fragmentList = new ArrayList<Fragment>();
       // 0全部 1'待结算', '2可结算', '3已结算'
        fragmentList.add(new AllMoneyCalculateFragment(qb));
        fragmentList.add(new AllMoneyCalculateFragment(djs));
        fragmentList.add(new AllMoneyCalculateFragment(kjs));
        fragmentList.add(new AllMoneyCalculateFragment(yjs));
        orderFragmentAdapter = new OrderFragmentAdapter(getSupportFragmentManager(), fragmentList);
        viewPager_viewpager.setAdapter(orderFragmentAdapter) ;
    }

    @Override
    protected void fillArrayInListView() {

    }
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(getClass().getSimpleName()); //统计页面(仅有Activity的应用中SDK自动调用，不需要单独写)
        MobclickAgent.onResume(this);          //统计时长
    }
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(getClass().getSimpleName()); // （仅有Activity的应用中SDK自动调用，不需要单独写）保证 onPageEnd 在onPause 之前调用,因为 onPause 中会保存信息
        MobclickAgent.onPause(this);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
      //  App.showToast(position+"onPageScrolled");
    }

    @Override
    public void onPageSelected(int position) {
        changeState();
        switch (position) {
            case 0:
                textView_all_pay.setTextColor(getResources().getColor(R.color.color_f875a3_red));
                view1.setVisibility(View.VISIBLE);
             //   ((AllOrderFragment) fragmentList.get(0)).refresh();
                break;
            case 1:
                textView_wait_pay.setTextColor(getResources().getColor(R.color.color_f875a3_red));
                view2.setVisibility(View.VISIBLE);
             //   ((PayOrderFragment) fragmentList.get(1)).refresh();
                break;
            case 2:
                textView_can_pay.setTextColor(getResources().getColor(R.color.color_f875a3_red));
                view3.setVisibility(View.VISIBLE);
            //    ((ConsumptionOrderFragment) fragmentList.get(2)).refresh();
                break;
            case 3:
                textView_end_pay.setTextColor(getResources().getColor(R.color.color_f875a3_red));
                view4.setVisibility(View.VISIBLE);
            //    ((EvaluationOrderFragment) fragmentList.get(3)).refresh();
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

        //App.showToast(state+"");
    }

    private void changeState() {
        textView_all_pay.setTextColor(getResources().getColor(R.color.color_808080_gray));
        textView_wait_pay.setTextColor(getResources().getColor(R.color.color_808080_gray));
        textView_can_pay.setTextColor(getResources().getColor(R.color.color_808080_gray));
        textView_end_pay.setTextColor(getResources().getColor(R.color.color_808080_gray));
        view1.setVisibility(View.GONE);
        view2.setVisibility(View.GONE);
        view3.setVisibility(View.GONE);
        view4.setVisibility(View.GONE);
    }
    public void onEventMainThread(MoneyControlBean moneyControlBean) {


        textView_djsbs1.setText("待结算笔数： "+moneyControlBean.getSum_data().getWait_settle_sum());
        textView_djsje1.setText("待结算金额： "+moneyControlBean.getSum_data().getWait_settle_amount()+"");
        textView_djsbs2.setText("可结算笔数： "+moneyControlBean.getSum_data().getCan_settle_sum()+"");
        textView_djsje2.setText("可结算金额： "+moneyControlBean.getSum_data().getCan_settle_amount()+"");
        textView_djsbs3.setText("已结算笔数： "+moneyControlBean.getSum_data().getFinish_settle_sum()+"");
        textView_djsje3.setText("已结算金额： "+moneyControlBean.getSum_data().getFinish_settle_amount()+"");
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId())
        {
            case R.id.textView_all_pay :
                viewPager_viewpager.setCurrentItem(0);
                break;
            case R.id.textView_wait_pay :
                viewPager_viewpager.setCurrentItem(1);
                break;
            case R.id.textView_can_pay :
                viewPager_viewpager.setCurrentItem(2);
                break;
            case R.id.textView_end_pay :
                viewPager_viewpager.setCurrentItem(3);
                break;

        }
    }
}
